package com.gonsin.gview.logic.parser;

import com.gonsin.gview.annotations.GContext;
import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.model.ViewRequest;
import com.gonsin.gview.logic.model.ClassView;
import com.gonsin.gview.logic.services.GViewCoreServiceImpl;
import com.gonsin.gview.model.GViewModel;
import com.gonsin.gview.model.IViewModelHolder;
import com.gonsin.gview.model.IViewPermissionHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

/**
 * 使用桥接模式
 * 这个是标准的，静态生成的viewModel，不需要处理任何工作，直接返回即可
 */
public class StanderModelHolder implements IViewModelHolder {

    private GView gview;

    private Class clazz;

    private String viewName;

    private List<String> props;

    private StanderParser parser;

    public StanderModelHolder(GView gview, Class clazz, StanderParser parser) {
        this.gview = gview;
        this.clazz = clazz;
        this.viewName = gview.viewName();
        this.props = Arrays.asList(gview.props());
        this.parser = parser;
    }

    @Override
    public GViewModel getViewModel(ViewRequest request, IViewPermissionHandler permissionHandler) {
        GContext context = new GContext(request.getViewName(), request.getRequest(), request.getResponse(), request.getProps(), request.isInitialing());
        return transformLanguage(parser.parse(context, new ClassView(viewName, gview)));
    }

    @Override
    public String getViewName() {
        return viewName;
    }

    @Override
    public List<String> getProps() {
        return props;
    }


    /**
     * 经过gonsin i18n转换语言
     */
    private GViewModel transformLanguage(GViewModel viewModel) {
        return viewModel.tClone();
    }
}
